package com.longrise.android.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.ShareParams;
import com.longrise.common.utils.AppBaseDialog;
import com.longrise.common.utils.DZZWTools;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by godliness on 2017/7/17.
 * 分享dialog
 */

public class ShareDialog extends AppBaseDialog implements View.OnClickListener {

    private static final String TAG = "ShareDialog";

    private ShareCallback mCallback;

    private LinearLayout mLLWeChat;
    private LinearLayout mLLWeChatMoments;
    private LinearLayout mLLQQ;
    private LinearLayout mLLQQZone;

    private ImageView mIvClose;

    private ShareParams mShareParams;

    public ShareDialog(Context context) {
        super(context, R.style.VideoListDialog);
        setCanceledOnTouchOutside(false);
    }

    public void setShareParame(ShareParams shareParame) {
        this.mShareParams = shareParame;
    }

    @Override

    public int getLayoutResourceId(Bundle bundle) {
        return R.layout.dialog_share;
    }

    @Override
    public void init() {
        mLLWeChat = (LinearLayout) findViewById(R.id.shareapp_wechat_ll);
        mLLWeChatMoments = (LinearLayout) findViewById(R.id.shareapp_pengyouquan_ll);
        mLLQQ = (LinearLayout) findViewById(R.id.shareapp_qq_ll);
        mLLQQZone = (LinearLayout) findViewById(R.id.shareapp_qzone_ll);

        mIvClose = (ImageView) findViewById(R.id.iv_share_dismiss);

        regEvent();
    }

    @Override
    public void regEvent() {
        if (mLLWeChat != null) {
            mLLWeChat.setOnClickListener(this);
        }
        if (mLLWeChatMoments != null) {
            mLLWeChatMoments.setOnClickListener(this);
        }
        if (mLLQQ != null) {
            mLLQQ.setOnClickListener(this);
        }
        if (mLLQQZone != null) {
            mLLQQZone.setOnClickListener(this);
        }
        if (mIvClose != null) {
            mIvClose.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shareapp_wechat_ll:
                share(2);
                break;

            case R.id.shareapp_pengyouquan_ll:
                share(3);
                break;

            case R.id.shareapp_qq_ll:
                share(4);
                break;

            case R.id.shareapp_qzone_ll:
                share(1);
                break;

            case R.id.iv_share_dismiss:
                dismiss();
                break;

            default:
                break;
        }
    }

    /**
     * 分享
     *
     * @param flag 标记
     */
    private void share(int flag) {
        if (mShareParams == null) {
            return;
        }
        ShareSDK.initSDK(getContext());
        Platform platform;
        Platform.ShareParams sp;
        if (flag == 1) {
            platform = ShareSDK.getPlatform(QZone.NAME);
            sp = new QZone.ShareParams();
        } else if (flag == 2) {
            platform = ShareSDK.getPlatform(Wechat.NAME);
            sp = new Wechat.ShareParams();
        } else if (flag == 3) {
            platform = ShareSDK.getPlatform(WechatMoments.NAME);
            sp = new WechatMoments.ShareParams();
        } else {
            platform = ShareSDK.getPlatform(QQ.NAME);
            sp = new QQ.ShareParams();
        }
        sp.setTitle(mShareParams.mTitle);
        sp.setTitleUrl(mShareParams.mUrl);
        sp.setUrl(mShareParams.mUrl);
        sp.setSiteUrl(mShareParams.mUrl);
        sp.setImageUrl(mShareParams.mHeadUrl);
        sp.setText(mShareParams.mContent);
        sp.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                DZZWTools.showToast(getContext(), "分享成功", 0);

                if (mCallback != null) {
                    mCallback.shareSuccess();
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                DZZWTools.showToast(getContext(), "分享失败", 0);

                if (mCallback != null) {
                    mCallback.shareFailed();
                }
            }

            @Override
            public void onCancel(Platform platform, int i) {
                DZZWTools.showToast(getContext(), "分享取消", 0);

                if (mCallback != null) {
                    mCallback.shareFailed();
                }
            }
        });
        dismiss();
        platform.share(sp);
    }

    public void setCallback(ShareCallback callback) {
        this.mCallback = callback;
    }

    public interface ShareCallback {
        void shareSuccess();

        void shareFailed();
    }
}
